//
//  CommonCell.m
//  Weibo
//
//  Created by Anasue on 9/21/15.
//  Copyright (c) 2015 Anasue. All rights reserved.
//

#import "CommonCell.h"
#import "CommonItem.h"
#import "CommonArrowItem.h"
#import "CommonSwitchItem.h"
#import "CommonLabelItem.h"
#import "BadgeValueView.h"

@interface CommonCell ()
/**
 *  箭头
 */
@property (strong, nonatomic) UIImageView *rightArrow;
/**
 *  开关
 */
@property (strong, nonatomic) UISwitch *rightSwitch;
/**
 *  标签
 */
@property (strong, nonatomic) UILabel *rightLabel;

@property (nonatomic, strong) BadgeValueView *badgeValueView;
@end

@implementation CommonCell
#pragma mark - 懒加载
- (UIImageView *)rightArrow
{
    if (_rightArrow == nil) {
        self.rightArrow = [[UIImageView alloc]initWithImage:[UIImage imageWithName:@"common_icon_arrow"]];
    }
    return _rightArrow;
}

- (UISwitch *)rightSwitch
{
    if (_rightSwitch == nil) {
        self.rightSwitch = [[UISwitch alloc]init];
    }
    return _rightSwitch;
}

- (UILabel *)rightLabel
{
    if (_rightLabel == nil) {
        self.rightLabel = [[UILabel alloc]init];
    }
    return _rightLabel;
}

- (BadgeValueView *)badgeValueView
{
    if (_badgeValueView == nil) {
        self.badgeValueView = [[BadgeValueView alloc]init];
    }
    return _badgeValueView;
}

#pragma mark - cell初始化
+ (instancetype)cellWithTableView:(UITableView *)tableView
{
    static NSString *identifier = @"cell";
    CommonCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
    if (cell == nil) {
        cell = [[CommonCell alloc]initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:identifier];
    }
    return cell;
}

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        self.textLabel.font = [UIFont boldSystemFontOfSize:15];
        self.detailTextLabel.font = [UIFont systemFontOfSize:11];
        self.backgroundView = [[UIImageView alloc]init];
        self.selectedBackgroundView = [[UIImageView alloc]init];
    }
    return self;    
}

- (void)layoutSubviews
{
    [super layoutSubviews];
    self.detailTextLabel.x = CGRectGetMaxX(self.textLabel.frame) + 5;
}

- (void)setIndexPath:(NSIndexPath *)indexPath rowsOfSection:(int)rows
{
    UIImageView *backgroundView = (UIImageView *)self.backgroundView;
    UIImageView *selectedView = (UIImageView *)self.selectedBackgroundView;
    
    if (1 == rows) {
        backgroundView.image = [UIImage resizeImage:@"common_card_background"];
        selectedView.image = [UIImage resizeImage:@"common_card_background_highlighted"];
    }else if (indexPath.row == 0){
        backgroundView.image = [UIImage resizeImage:@"common_card_top_background"];
        selectedView.image = [UIImage resizeImage:@"common_card_top_background_highlighted"];
    }else if (indexPath.row == rows - 1){
        backgroundView.image = [UIImage resizeImage:@"common_card_bottom_background"];
        selectedView.image = [UIImage resizeImage:@"common_card_bottom_background_highlighted"];
    }else{
        backgroundView.image = [UIImage resizeImage:@"common_card_middle_background"];
        selectedView.image = [UIImage resizeImage:@"common_card_middle_background_highlighted"];
    }
}

- (void)setItem:(CommonItem *)item
{
    _item = item;
    self.imageView.image = [UIImage imageWithName:item.icon];
    self.textLabel.text = item.title;
    self.detailTextLabel.text = item.subTitle;
    
    if (item.badgeValue) {
        self.badgeValueView.badgeValue = item.badgeValue;
        self.accessoryView = self.badgeValueView;
    }else if ([item isKindOfClass:[CommonArrowItem class]]) {
        self.accessoryView = self.rightArrow;
    }else if ([item isKindOfClass:[CommonSwitchItem class]]){
        self.accessoryView = self.rightSwitch;
    }else if ([item isKindOfClass:[CommonLabelItem class]]){
        CommonLabelItem *labelItem = [[CommonLabelItem alloc]init];
        self.rightLabel.text = labelItem.title;
        self.rightLabel.size = [labelItem.title sizeWithFont:self.rightLabel.font];
        self.accessoryView = self.rightLabel;
    }else{
        self.accessoryView = nil;
    }
}


@end
